let { getDataController, getDataControllerUnPage } = require('../controller/publicController');
let {
    delClassByIdController,
    addClassController,
    findClassByNameController,
    updateClassController,
    findClassByIdController,
    findClassByNameFilterController
} = require('../controller/classController');

getClassListService = async (req, res) => {
    let { pageIndex, pageSize } = req.query;
    if (pageIndex && pageIndex > 0 && pageSize && pageSize > 0) {
        let results = await getDataController(role = "class", populate = null, pageIndex, pageSize);
        if (results.total) {
            res.json({
                code: 200,
                message: "数据查询成功",
                total: results.total,
                data: results.results
            })
        } else {
            res.json({
                code: 201,
                message: "数据查询失败",
            })
        }
    } else {
        res.json({
            code: 201,
            message: "数据查询失败，参数不正确",
        })
    }

}

getClassListUnPageService = async (req, res) => {
    let results = await getDataControllerUnPage(role = "class", populate = null);
    if (results.total) {
        res.json({
            code: 200,
            message: "数据查询成功",
            total: results.total,
            data: results.results
        })
    } else {
        res.json({
            code: 201,
            message: "数据查询失败",
        })
    }
}

delClassByIdService = async (req, res) => {
    let { id } = req.body;
    if (id) {
        let result = await delClassByIdController(id);
        if (result.deletedCount == 1) {
            res.json({
                code: 200,
                message: "删除成功"
            })
        } else {
            res.json({
                code: 201,
                message: "删除失败，未知原因"
            })
        }
    } else {
        res.json({
            code: 201,
            message: "删除失败，参数不正确"
        })
    }
}

addClassByIdService = async (req, res) => {
    let { name } = req.body;
    if (name) {
        let findClassByName = await findClassByNameController(name);
        console.log(findClassByName);
        if (findClassByName.length) {
            res.json({
                code: 201,
                message: "班级新增失败,该班级已经存在"
            })
        } else {
            let result = await addClassController(name)
            if (result) {
                res.json({
                    code: 200,
                    message: "班级新增成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "班级新增失败,未知异常"
                })
            }
        }
    } else {
        res.json({
            code: 201,
            message: "班级新增失败,参数传递异常"
        })
    }
}

updateClassByIdService = async (req, res) => {
    let { id, name } = req.body;
    if (id && name) {
        let findClassById = await findClassByIdController(id);
        if (findClassById) {
            let result = await updateClassController(id, name)
            if (result.matchedCount == 1) {
                res.json({
                    code: 200,
                    message: "班级更新成功"
                })
            } else {
                res.json({
                    code: 201,
                    message: "班级更新失败,未知异常"
                })
            }
        } else {
            res.json({
                code: 201,
                message: "班级更新失败,该班级不存在"
            })
        }
    } else {
        res.json({
            code: 201,
            message: "班级更新失败,参数传递异常"
        })
    }
}

findClassByNameFilterService= async (req, res) => {
    let { name } = req.body;
    if (!name) {
        name = ""
    }
    let results = await findClassByNameFilterController(name);
    if (results.length) {
        res.json({
            code: 200,
            message: "数据查询成功",
            data: results
        })
    } else {
        res.json({
            code: 201,
            message: "数据查询失败",
        })
    }
}

module.exports = {
    getClassListService,
    getClassListUnPageService,
    delClassByIdService,
    addClassByIdService,
    updateClassByIdService,
    findClassByNameFilterService
}